package com.kaibai.controller;

import com.kaibai.pojo.Cart;
import com.kaibai.pojo.Order;
import com.kaibai.pojo.OrderItem;
import com.kaibai.pojo.User;
import com.kaibai.service.OrderService;
import com.kaibai.service.impl.OrderServiceImpl;
import com.kaibai.utils.WebUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    protected void creatOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取cart购物车对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //2.获取userid
        User user = (User) request.getSession().getAttribute("user");
        Integer uid = user.getId();
        String orderId = orderService.createOrder(cart,uid);
        if(orderId != null){
            request.getSession().setAttribute("orderId",orderId);
        }else{
            request.getSession().setAttribute("orderId","商品库存不足");
        }

//        request.getRequestDispatcher("/jsp/cart/checkout.jsp").forward(request,response);
        response.sendRedirect(request.getContextPath()+"/jsp/cart/checkout.jsp");
    }

    protected void creatOrderDB(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int uid = WebUtil.parseInt(request.getParameter("uId"),0);
        int totalPrice = WebUtil.parseInt(request.getParameter("totalPrice"),0);
        String orderId = orderService.createOrderDB(uid,totalPrice);
        if(orderId != null){
            request.getSession().removeAttribute("orderError");
            request.getSession().setAttribute("orderId",orderId);
        }else{
            request.getSession().removeAttribute("orderId");
            request.getSession().setAttribute("orderError","商品库存不足");
        }
        response.sendRedirect(request.getContextPath()+"/jsp/cart/checkout.jsp");
    }

    protected void showMyOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Integer userid = WebUtil.parseInt(request.getParameter("userId"),0);
        List<Order> list = orderService.showMyOrders(userid);
        request.getSession().setAttribute("orderlist",list);
        response.sendRedirect(request.getContextPath()+"/jsp/order/order.jsp");
    }

    protected void showOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String orderid = request.getParameter("orderid");
        List<OrderItem> list = orderService.showOrderDetail(orderid);
        request.getSession().setAttribute("orderItemList",list);
        response.sendRedirect(request.getContextPath()+"/jsp/order/checkorder.jsp");
    }

    protected void showAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        List<Order> list = orderService.showAllOrders();

        request.getSession().setAttribute("orderlist",list);
        response.sendRedirect(request.getContextPath()+"/jsp/manager/order_manager.jsp");
    }
    protected void sendOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String orderid = request.getParameter("orderid");
        int res = orderService.sendOrder(orderid);
        response.sendRedirect(request.getContextPath()+"/order?action=showAllOrders");
    }
    protected void receiverOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String orderid = request.getParameter("orderid");
        int res = orderService.receiverOrder(orderid);
        User user = (User) request.getSession().getAttribute("user");
        response.sendRedirect(request.getContextPath()+"/order?action=showMyOrders&userId="+user.getId());
    }
}
